package common

import (
	"fmt"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
	"test/model"
)

var DB *gorm.DB
func InitDB() *gorm.DB {//建立数据库连接
	var err error
	args := fmt.Sprintf("user=%s password=%s dbname=%s host=%s port=%s sslmode=disable TimeZone=Asia/Shanghai",
		"postgres",
		"dujiahui",
		"Training",
		"localhost",
		"5432",
	)
	db,err:= gorm.Open(postgres.Open(args), &gorm.Config{})
	if err != nil {
		panic("faied to connect database,err:" + err.Error())
	}
	db.AutoMigrate(&model.Students{})
	DB = db
	return db
}

func GetDB() *gorm.DB {
	return DB
}
